package com.jk.mindskip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.mindskip.dao.TbAdminMapper;
import com.jk.mindskip.dto.TbAdmin;
import com.jk.mindskip.pojo.PagedResponse;
import com.jk.mindskip.service.TbAdminService;
import com.jk.mindskip.utils.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TbAdminServiceImpl implements TbAdminService {

    @Autowired
    private TbAdminMapper adminMapper;

    @Override
    public PagedResponse<TbAdmin> findAdminList(TbAdmin admin, Integer pageNum, Integer pageSize) {
        Page<TbAdmin> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TbAdmin> queryWrapper = new QueryWrapper<>();
        Page<TbAdmin> adminPage = adminMapper.selectPage(page, queryWrapper);
        return PaginationUtil.getPaginationResponse(adminPage.getRecords(), pageNum, pageSize, adminPage.getTotal());
    }

    @Override
    public void addAdmin(TbAdmin admin) {
        adminMapper.insert(admin);
    }

    @Override
    public TbAdmin findTbAdminByUserName(String username) {
        QueryWrapper<TbAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", username);
        return adminMapper.selectOne(queryWrapper);
    }
}
